//
//  BJBlockListViewController.m
//  BowJoy
//
//  Created by  on 2024/5/14.
//

#import "BJBlockListViewController.h"
#import "BJFolloewListTableViewCell.h"
#import "BJBlockListModel.h"
#import "BJShowEmptyView.h"

@interface BJBlockListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic) NSMutableArray<BJBlockListModelData *> *dataList;
@property (nonatomic,strong)BJShowEmptyView  *showEmptyView;

@end

@implementation BJBlockListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviTitle = @"Block List";

    
    self.view.addChild(self.tableView);
    self.tableView.makeCons(^{
        make.leading.trailing.constants(0);
        make.bottom.view(self.view).bottom.constants(-90);
        make.top.view(self.naviContentView).bottom.constants(0);
    });

    [self getData];
}



- (void)getData{
    kWeakSelf(weakSelf)
    [[BJNetManager shareNetManager] getBlockListWithCompletionHandler:^(BJBlockListModel * _Nonnull model, NSError * _Nonnull error) {
        if(!error){


            [weakSelf.dataList removeAllObjects];
            [weakSelf.dataList addObjectsFromArray:model.data];
         }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView endHeaderRefresh];
    }];

}

- (NSMutableArray<BJBlockListModelData *> *)dataList{
    AsGetMethodReturnObjc(_dataList);
    _dataList = [NSMutableArray array];
    return _dataList;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BJFolloewListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BJFolloewListTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.blockBtn setImage:[UIImage imageNamed:@"icon_blockList_btn"] forState:UIControlStateNormal];
    if(self.dataList.count>indexPath.item){
        BJBlockListModelData *model = self.dataList[indexPath.row];
        cell.nikeNameL.text = model.nickName;
        [cell.headerIcon setImageURL:[NSURL URLWithString:model.avatar]];
    }
    kWeakSelf(weakSelf)
    cell.blockBtn.onClick(^{
        BJBlockListModelData *model = self.dataList[indexPath.row];
        [[BJNetManager shareNetManager]postRemoveBlockWithBlockUserId:model.broadcasterId completionHandler:^(id  _Nonnull responOj, NSError * _Nonnull error) {
            NSDictionary *dict = responOj;
            NSInteger code = [dict[@"code"]integerValue];
            if(!error && code == 0){
                [ASHUD showWithText:@"Remove Successfully" afterDelay:2];
                [BJUserManager sharedManager].blockNum --;

                NSArray *dataQueryList = [[BJFMDBBlockManager sharedManager] queryAllData];
                for (BJAnchorWallModelData *item in dataQueryList) {
                    if ([item.userId isEqualToString:[NSString stringWithFormat:@"%ld",(long)model.broadcasterId]]) {
                        if ([item.playurl isEqualToString:@""]) {
                            [[BJFMDBManager sharedManager] insertWitModel:item];
                        }else{
                            [[BJFMDBVoiceManager sharedManager] insertWitModel:item];
                        }
                    }
                }
                [weakSelf getData];
            }
        }];

    });

    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}


- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerNib:[UINib nibWithNibName:@"BJFolloewListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BJFolloewListTableViewCell"];
    }
    return _tableView;
}

@end
